﻿using GraphQL;
using GraphQL.DataLoader;
using GraphQLDemo.Domain.Companies;
using GraphQLDemo.Domain.CompanyProjects;
using GraphQLDemo.Domain.Contracts;
using GraphQLDemo.Domain.Orders;
using GraphQLDemo.Infrastructure.Repositories;

namespace GraphQLDemo.Api.Extensions;

public static class GraphQLExtensions
{
    public static void AddGraphQLService(this IServiceCollection services)
    {
        var graphQLSetting = GraphQLSettings.DefaultGraphQLSettings();
        services.AddGraphQL(b => b
            .AddSchema<QuerySchema>()
            .AddSystemTextJson()
            .AddAuthorizationRule()
            .AddUserContextBuilder(graphQLSetting.BuildUserContext)
            .AddGraphTypes(typeof(QuerySchema).Assembly)
            .UseApolloTracing(graphQLSetting.EnableMetrics));
        services.AddSingleton(graphQLSetting);
        services.AddSingleton<DataLoaderDocumentListener>();
        services.AddSingleton<IDataLoaderContextAccessor, DataLoaderContextAccessor>();
    }

    public static void AddQueryService(this IServiceCollection services)
    {
    }

    public static void AddRepositoryService(this IServiceCollection services)
    {
        services.AddSingleton<ICompanyProjectRepository, CompanyProjectRepository>();
        services.AddSingleton<ICompanyRepository, CompanyRepository>();
        services.AddSingleton<IOrderRepository, OrderRepository>();
        services.AddSingleton<IContractRepository, ContractRepository>();
    }
}